package src.com.qf.service.impl;

import src.com.qf.dao.GoodDao;
import src.com.qf.dao.GoodTypeDao;
import src.com.qf.dao.impl.GoodDaoImpl;
import src.com.qf.dao.impl.GoodTypeDaoImpl;
import src.com.qf.entity.Good;
import src.com.qf.entity.GoodType;
import src.com.qf.entity.Page;
import src.com.qf.service.GoodService;
import src.com.qf.utils.JdbcUtil;

import java.util.List;

public class GoodServiceImpl implements GoodService {
    GoodDao goodDao = new GoodDaoImpl();
    private GoodTypeDao goodTypeDao = new GoodTypeDaoImpl();
    @Override
    public List<Good> getGoodsListByTypeId(Integer typeId, Page page) {
        List<Good> list =null;
        try{
            JdbcUtil.begin();
            //计算总记录数应是当前分类下的总记录数，而不是总的总记录数
            page.setTotalCounts(goodDao.selectCount(typeId));

            list = goodDao.selectLitmitGoodsByTypeId(typeId,page);

            JdbcUtil.commit();
        }catch (Exception e){
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Good showGoodDetail(int id) {
        Good good = null;
        try{
            JdbcUtil.begin();
            good = goodDao.selectGoodById(id);
            //根据查询得到的对象的typeId去表中将商品分类对象查询出来
            GoodType goodType = goodTypeDao.selectGoodTypeById(good.getTypeId());
            good.setGoodType(goodType);
            JdbcUtil.commit();
        }catch (Exception e){
            JdbcUtil.rollback();
            e.printStackTrace();
        }

        return good;
    }
}
